//
//  TTHomeCellModel.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/23.
//  Copyright © 2016年 陶路路. All rights reserved.
//

/*
var verified: Int = 0
/// 会员等级 1-6
var mbrank: Int = 0
*/

import UIKit

class TTHomeCellModel: NSObject {
    
    var cellModel:TTHomeVcMod?
    
    var memberImageView:UIImage?
    
    var avatarImageView:UIImage?
    
    var souce:NSAttributedString?
    
    // 转发数
    var reposts_count: String?
    // 评论数
    var comments_count: String?
    // 表态数
    var attitudes_count: String?
    
    var attribute : NSMutableAttributedString?
    
    init(model:TTHomeVcMod) {
        
        super.init()
        
        self.cellModel = model
        
        memberImageView = getMemberImageView(cellModel?.user?.verified_type)
        avatarImageView = getAvatarImageView(cellModel?.user?.mbrank)
        souce = getSouce(cellModel?.source)
        reposts_count = getCount(model.reposts_count, title: "转发")
        comments_count = getCount(model.comments_count, title: "评论")
        attitudes_count = getCount(model.attitudes_count, title: "赞")
        
// 调用方法将微博文章转为富文本
        
        attribute = getAttributeString(model.text)
    }
    
    // 处理微博认证用户图片/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人

    func getMemberImageView(num:Int?)->UIImage?{
        
        guard let number = num else{
            
            return nil
        }
        
        switch number {
            
        case 1:
            return UIImage(named: "avatar_vip")
            
        case 2,3,5:
            
            return UIImage(named: "avatar_grassroot")
            
        case 220:
            return UIImage(named: "avatar_vgirl")
            
        default:
            return UIImage(named: "avatar_enterprise_vip")
        }
    }
     // 处理会员等级图片/// 会员等级 1-6
    
    func getAvatarImageView(num:Int?)->UIImage?{
        
        guard let number = num else{
            
            return nil
        }
        
        if number > 0 && number < 7{
            
            return UIImage(named: "common_icon_membership_level\(number)")
        }else{
            
            return UIImage(named: "common_icon_membership_expired")
        }
        
        
    }


}

//Optional("<a href=\"http://app.weibo.com/t/feed/4fuyNj\" rel=\"nofollow\">即刻笔记</a>")

// mark:获取来源和时间
extension TTHomeCellModel{
    
    func getSouce(souce:String?)->NSMutableAttributedString{
        
        guard let str = souce where str.containsString("</") else{
            
            return getAttributedString("大强微博")
        }
        
// mark : 截取字符串
        
        let preRange = str.rangeOfString("\">")!
        let lastRange = str.rangeOfString("</")!
        
        let string = str.substringWithRange((preRange.endIndex)..<(lastRange.startIndex))
        
        return getAttributedString(string)
    }
// mark: 富文本
    func getAttributedString(souce:String)->NSMutableAttributedString{
        
        let name = "来自 " + souce
        let arrt = NSMutableAttributedString(string: name)
        
        let range = (name as NSString).rangeOfString("来自")
        
        arrt.addAttributes([NSForegroundColorAttributeName:UIColor.grayColor()], range: range)
        
        return arrt
    }
    
// 获取 转发 评论 点赞人数
    func getCount(count:Int,title:String)->String{
        
        if count <= 0 {
            
            return title
            
        }else{
            
            if count < 10000{
                
                return "\(count)"
                
            }else{
                
                let num = CGFloat(count/1000) / 10
                var numString = "\(num)万"
                if numString.containsString(".0"){
                    
                    numString = numString.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                
                return numString
            }
        }
        
    }
    
    
    //    把微博文本转为富文本
    
    func getAttributeString(lable:String?)->NSMutableAttributedString?{
        
        guard let text = lable else{
            
            return nil
        }
        
        //        得到一个富文本
        let textAttribut = NSMutableAttributedString(string: text)
        //        正则格式
        let regex = "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
//        创建临时数组
        var tempArray : [TTMatchResultModel] =  [TTMatchResultModel]()
        //        遍历富文本
        (text as NSString).enumerateStringsMatchedByRegex(regex) { (count, string, range, _) -> Void in
            
            let matchModel = TTMatchResultModel(string: string.memory as? String, range: range.memory)
            
            tempArray.append(matchModel)
        }
        
        for model in tempArray.reverse(){
            
            guard let emotionModel = TTEmotionTools.sharedEmotionTools.getEmotoinModel(model.string!) else{
                
                continue
            }
            
            let lineHight = UIFont.systemFontOfSize(15).lineHeight
            
            let attribute = NSAttributedString(model: emotionModel, emotionHight: lineHight)
            
            textAttribut.replaceCharactersInRange(model.range, withAttributedString: attribute)
        }
        
        return textAttribut
        
    }

}
